package com.yxt.shecaiyun.controller;
//用户信息管理（增删改查）
import com.yxt.shecaiyun.common.PageResult;
import com.yxt.shecaiyun.common.R;
import com.yxt.shecaiyun.dto.userDto.UserQueryDTO;
import com.yxt.shecaiyun.dto.userDto.UserUpdateDTO;
import com.yxt.shecaiyun.entity.User;
import com.yxt.shecaiyun.service.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController extends BaseController<UserService, User> {
    @Autowired
    private UserService userService;
    @PostMapping("/pageSelect")
    public PageResult<User> pageSelect(@Valid @RequestBody UserQueryDTO query) {
        return userService.getUserPage(query);
    }
    // 根据用户id查询用户信息
    @GetMapping("/{userId}")
    public R getUserById(@PathVariable Long userId) {
        User user = userService.getById(userId);
        return R.success(user);
    }

//    @PutMapping("/{userId}")
//    public R updateUser(
//            @PathVariable Long userId,
//            @Valid @RequestBody UserUpdateDTO dto) {
//        User user = userService.getById(userId);
//        userService.update(user, dto);
//        return R.success("更新成功");
//    }
    @PutMapping("/{userId}")
    public R updateUser(
            @PathVariable Long userId,
            @Valid @RequestBody UserUpdateDTO dto) {
        User user = userService.getById(userId);
        if (user == null) {
            return R.fail("用户不存在");
        }
        userService.updateUser(user, dto);
        return R.success("更新成功");
    }
//    禁用/启用
    @PutMapping("/{id}/status")
    public R updateStatus(
            @PathVariable Long id,
            @RequestBody Map<String, Object> requestBody
    ) {
        Integer newStatus = (Integer) requestBody.get("active");
        User user = userService.getById(id);
        user.setActive(newStatus);
        user.setUpdatedAt(LocalDateTime.now());

        // 使用 updateById 根据ID更新实体
        userService.updateById(user);

        return R.success("更新成功");
    }


}

